package com.fyp.ptma.model;

import java.io.Serializable;

import javax.persistence.Embeddable;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.Transient;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@Embeddable
@XmlRootElement(name = "versionPk")
@XmlAccessorType(XmlAccessType.NONE)
public class ApplicationVersionPk implements Serializable {

	/**
     * 
     */
	private static final long serialVersionUID = 1L;

	// @XmlElement
	private Application application;
	@XmlElement(name = "application")
	private Application app;

	@XmlElement
	private String version;

	public ApplicationVersionPk() {

	}

	public ApplicationVersionPk(Application app, String version) {
		this.application = app;
		this.version = version;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({ @JoinColumn(referencedColumnName = "id", nullable = false) })
	public Application getApplication() {
		return application;
	}

	@Transient
	public Application getApp() {
		return app;
	}

	public void setApp(Application app) {
		this.app = app;
	}

	public void setApplication(Application application) {
		this.application = application;
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public boolean equals(Object other) {
		if ((this == other))
			return true;
		if ((other == null))
			return false;
		if (!(other instanceof ApplicationVersionPk))
			return false;
		ApplicationVersionPk versionPk = (ApplicationVersionPk) other;

		return (this.getApplication().getNameSpace()
				.equalsIgnoreCase(versionPk.getApplication().getNameSpace()) && this.version
				.equalsIgnoreCase(versionPk.getVersion()));
	}

	public int hashCode() {
		int result = 17;

		result = result + this.version.hashCode();
		result += result + this.getApplication().getNameSpace().hashCode();
		return result;
	}

}